<template>
    <div class="upload-own-img-com" :style="{'width': width, 'height': height, 'overflow': overflow}">
        <!-- <div class="img-box">
                <i slot="default" class="el-icon-plus"></i>
        </div> -->
        <el-upload
            class="avatar-uploader"
            :style="{'width': width, 'height': height}"
            action="/admin/file/uploadPic"
            :show-file-list="false"
            :data="data"
            accept=".jpg,.jpeg,.png"
            :on-success="handleAvatarSuccess">
            <div>
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </div>
        </el-upload>
    </div>
</template>
<script>
export default {
    name: 'uploadOwnImg',
    data () {
        return {
            imageUrl: '',
            data: {}
        }
    },
    props: {
        value: {},
        needTransform: {},
        width: {
            default: '80px'
        },
        height: {
            default: '80px'
        },
        overflow: {
            default: 'initial'
        }
    },
    created () {
        // 如果需要压缩
        if (this.needTransform) {
            this.data.needTransform = 1
        }
        this.imageUrl = this.value
    },
    methods: {
        handleAvatarSuccess (res, file) {
            if (res.code === '0') {
                this.imageUrl = res.data
                this.$emit('input', res.data)
            } else {
                this.$message({
                    message: '系统繁忙，请好后再试',
                    type: 'error'
                })
            }
        }
    },
    watch: {
        value (val) {
            this.imageUrl = val
        }
    }
}
</script>

<style lang="scss">
    .upload-own-img-com {
        display: flex;
        width: 80px;
        height: 80px;
        align-items: center;
        .avatar-uploader {
            overflow: hidden;
            > div {
                display: flex;
                align-items: center;
                justify-content: center;
            }
        }
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            height: 100%;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            height: 80px;
            width: 80px;
            line-height: 80px;
            text-align: center;
        }
        .avatar {
            width: 100%;
            display: block;
        }
    }
</style>
